package com.share.device.controller;

import com.share.common.core.web.domain.AjaxResult;
import com.share.common.security.annotation.RequiresLogin;
import com.share.device.domain.ScanChargeVo;
import com.share.device.domain.StationVo;
import com.share.device.service.IDeviceService;
import com.share.device.service.IStationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//查询当前位置附近范围内的站点 controller
@Tag(name = "设备接口管理")
@RestController
@RequestMapping("/device")
@Slf4j
public class DeviceController {

    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IStationService stationService;

    //查询当前位置附近范围内的站点 参数纬度 经度
    @Operation(summary = "获取附近站点信息列表")
    @GetMapping("/nearbyStationList/{latitude}/{longitude}")
    public AjaxResult nearbyStation(@PathVariable String latitude,
                                    @PathVariable String longitude){
        List<StationVo> stationVoList = deviceService.nearbyStation(latitude,longitude);
        return AjaxResult.success(stationVoList);
    }


    @Operation(summary = "根据经纬度搜索附近门店（站点）")
    @GetMapping("/nearbyStation/{latitude}/{longitude}")
    public AjaxResult nearStation(@PathVariable String latitude,
                                    @PathVariable String longitude) {

        log.info("根据经纬度搜索附近门店:{},{}",latitude,longitude);
        List<StationVo> stationVoList = deviceService.nearbyStation(latitude, longitude);
        if (stationVoList == null) {
            stationService.updateData();
        }
        return AjaxResult.success(stationVoList);
    }

    //传入经纬度参数是计算距离
    @Operation(summary = "根据id获取门店详情")
    @GetMapping("/getStation/{id}/{latitude}/{longitude}")
    public AjaxResult getStationDetail(@PathVariable Long id,@PathVariable String latitude,@PathVariable String longitude){
        log.info("根据id获取门店详情：{}",id);

        StationVo stationVo = deviceService.getStationDetail(id,latitude,longitude);

        return AjaxResult.success(stationVo);
    }

    /**
     * 扫码充电后触发 接口
     * 传入路径参数 被扫描的柜机编号
     */
    @Operation(summary = "扫码充电")
    @RequiresLogin
    @GetMapping("scanCharge/{cabinetNo}")
    public AjaxResult scanCharge(@PathVariable String cabinetNo){
        log.info("扫码充电:{}",cabinetNo);

        ScanChargeVo scanChargeVo = deviceService.scanCharge(cabinetNo);

        return AjaxResult.success(scanChargeVo);
    }

}
